package com.cloudspace.jindun.activity.dianhua;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.adapter.HistoryContactAdapter;
import com.cloudspace.jindun.dao.PhoneRecord;
import com.cloudspace.jindun.manager.ContactPersonManager;
import com.cloudspace.jindun.manager.PhoneRecordManager;
import com.cloudspace.jindun.model.Person;
import com.cloudspace.jindun.utils.CallingUtils;
import com.cloudspace.jindun.utils.Convert;

import java.util.List;

/**
 * Created by plato on 2016/12/27.
 */

public class HistoryActivity extends CustomTitleBaseActivity {
    public SwipeMenuListView mListView;
    private List<PhoneRecord> persons;

    private HistoryContactAdapter adpater;

    private void initData() {
        persons = PhoneRecordManager.getInstance().queryAllRecords();
        adpater.setDatas(persons);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_history, true);

        getTitleBar().setSimpleMode(getResources().getString(R.string.history));
        initView();

        adpater = new HistoryContactAdapter();
        initData();
        mListView.setAdapter(adpater);
    }

    private void initView() {
        mListView = (SwipeMenuListView) findViewById(R.id.phone_listview_history);

        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());
                openItem.setBackground(new ColorDrawable(getResources().getColor(R.color.color_c8c7cd)));
                openItem.setWidth(Convert.dip2px(73));
                openItem.setTitle(getResources().getString(R.string.call_history_cancel));
                openItem.setTitleSize(16);
                openItem.setHeight(Convert.dip2px(57));
                openItem.setTitleColor(Color.WHITE);
                openItem.setMarginBottom(Convert.dip2px(3));
                menu.addMenuItem(openItem);

                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                deleteItem.setBackground(new ColorDrawable(getResources().getColor(R.color.color_ff3a31)));
                deleteItem.setWidth(Convert.dip2px(73));
                deleteItem.setTitleColor(Color.WHITE);
                deleteItem.setTitleSize(16);
                deleteItem.setHeight(Convert.dip2px(57));
                deleteItem.setTitle(getResources().getString(R.string.call_history_del));
                deleteItem.setMarginBottom(Convert.dip2px(3));
                menu.addMenuItem(deleteItem);
            }
        };

        mListView.setMenuCreator(creator);
        mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        break;

                    case 1:
                        deleteARecord(position);
                        break;

                    default:
                        break;
                }

                return false;
            }
        });
        mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (persons != null) {
                    PhoneRecord phoneRecord = persons.get(position);
                    Person person = ContactPersonManager.getInstance().queryByUserId(phoneRecord.getUserid());
                    if (person != null) {
                        CallingUtils.showCallMethodDialog(HistoryActivity.this, person);
                    }
                }
            }
        });
    }

    private void deleteARecord(int position) {
        PhoneRecordManager.getInstance().deletePhoneRecord(persons.get(position).getUserid());
        initData();
    }


}
